package raceReservationServer.saxParser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RaceServerGatewaysSAXHandler extends DefaultHandler
{
	List<GatewayObject> gatewaysList = new ArrayList<GatewayObject>();
	GatewayObject gatewayObject;
	String text;
	

	public RaceServerGatewaysSAXHandler() {
		super();
	}


	public List<GatewayObject> getGatewaysList() {
		return gatewaysList;
	}



	public void setGatewaysList(List<GatewayObject> gatewaysList) {
		this.gatewaysList = gatewaysList;
	}
	

	public GatewayObject getGatewayObject() {
		return gatewayObject;
	}

	/*
	* Called when the Parser starts parsing the Current XML File.
	*/
	public void startDocument () throws SAXException
	{
		System.out.println("Parsing de document...");
		gatewayObject = new GatewayObject();
		text = "";
	}

	/*
	 *Called when the Parser Completes parsing the Current XML File.
	*/
	public void endDocument () throws SAXException
	{
		System.out.println("Parse finished");
	}

	/*
	 * Called when the starting of the Element is reached. For Example if we have Tag
	 * called <Title> ... </Title>, then this method is called when <Title> tag is
	 * Encountered while parsing the Current XML File. The AttributeList Parameter has
	 * the list of all Attributes declared for the Current Element in the XML File.
	*/
	public void startElement (String uri, String localName, String qName,
            Attributes attributes) throws SAXException
	{
		System.out.println(" * [Label] -> <" + qName + ">");

		if (qName.equals("gateway")) {
			this.gatewayObject=new GatewayObject();
			this.gatewaysList.add(this.gatewayObject);
		} else if (qName.equals("detail")) {
			String name=(attributes.getValue("name"));
			this.gatewayObject.setDetails(name);
		}
	}

	/*
	 * Called when the Ending of the current Element is reached. For example in the
	 * above explanation, this method is called when </Title> tag is reached
	*/
	public void endElement (String uri, String lName, String qName ) throws SAXException
	{
		if(this.text.length() > 0)
		{
			System.out.println("	- [Texto] -> " + text);
			if(qName.equals("technology"))
			 {
			 	this.gatewayObject.setTechnology(this.text);
			 	System.out.println(this.text);
			 }
			else if(qName.equals("serviceName"))
			{
				this.gatewayObject.setServiceName(this.text);
			}
		}
		this.text = "";
		System.out.println(" * [Etiqueta] -> <" + qName + ">");
	}
	
	public void characters(char[] ch, int start, int length)
	throws SAXException
	{
		String s = String.valueOf(ch, start, length).trim();
		this.text += s;
	}
}

